/**颜色小课堂
RGB代表红(Red)、绿(Green)、蓝(Blue)三个颜色通道。这是一种加色混色模型。RGB模型 通过调节红、绿、蓝三个通道的强度比例,可以形成各种颜色。
HSL是色相(Hue)、饱和度(Saturation)、亮度(Lightness)的缩写,明暗度只与L有关系
HSV是色相(Hue)、饱和度(Saturation)、亮度(Value)的缩写,这是一种更接近人对颜色感知的模型
HSB是色相(Hue)、饱和度(Saturation)、亮度(Brightness)的缩写,与HSV类似
CMYK是色料模型,代表青色(Cyan)、洋红(Magenta)、黄色(Yellow)和黑色(Key)。它常用于彩色打印
HSB 和 HSV 是同一个东西，只是名称不同
HSB 中的 S 控制纯色中混入白色的量，值越大，白色越少，颜色越纯； 
HSB 中的 B 控制纯色中混入黑色的量，值越大，黑色越少，明度越高 
HSL 中的 S 和黑白没有关系，饱和度不控制颜色中混入黑白的多寡； 
HSL 中的 L 控制纯色中的混入的黑白两种颜色。
author：ywx620
web:https://ywx620.gitee.io/learn/
time：2018-04-23
*/
window.ColorManager = {}
// HSV(HSB)到RGB的转换
ColorManager.hsvToRgb = (h, s, v) => {
    let r, g, b;
    const i = Math.floor(h * 6);
    const f = h * 6 - i;
    const p = v * (1 - s);
    const q = v * (1 - f * s);
    const t = v * (1 - (1 - f) * s);
    switch (i % 6) {
        case 0: r = v, g = t, b = p; break;
        case 1: r = q, g = v, b = p; break;
        case 2: r = p, g = v, b = t; break;
        case 3: r = p, g = q, b = v; break;
        case 4: r = t, g = p, b = v; break;
        case 5: r = v, g = p, b = q; break;
    }

    return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
// HSV(HSB)到HSB的转换  
ColorManager.rgbToHsv = (r, g, b) => {
    r /= 255, g /= 255, b /= 255;
    const max = Math.max(r, g, b), min = Math.min(r, g, b);
    let h, s, v = max;
    const d = max - min;
    s = max == 0 ? 0 : d / max;
    if (max == min) {
        h = 0;
    } else {
        switch (max) {
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
            case g: h = (b - r) / d + 2; break;
            case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
    }
    return [h, s, v];
}
// CMYK到RGB的转换
ColorManager.cmykToRgb = (c, m, y, k) => {
    let r = 255 * (1 - c) * (1 - k);
    let g = 255 * (1 - m) * (1 - k);
    let b = 255 * (1 - y) * (1 - k);
    return [Math.round(r), Math.round(g), Math.round(b)];
}
// RGB到CMYK的转换
ColorManager.rgbToCmyk = (r, g, b) => {
    if(r==0&&g==0&&b==0) return [0,0,0,1];
    r /= 255;
    g /= 255;
    b /= 255;
    let k = 1 - Math.max(r, g, b);
    let c = (1 - r - k) / (1 - k);
    let m = (1 - g - k) / (1 - k);
    let y = (1 - b - k) / (1 - k);
    return [c, m, y, k];
}
// RGB到CMYK的转换正数
ColorManager.rgbToCmyk2 = (r, g, b) => {
    let cmyk=ColorManager.rgbToCmyk(r,g,b);
    cmyk=cmyk.map(function(a){return Math.floor(a*100)})
    return cmyk;
}
/**HSL到RGB的转换(L值越大颜色越亮，L值越小颜色越暗)*/
ColorManager.hslToRgb = (h, s, l) => {
    let r, g, b;
    if (s == 0) {
        r = g = b = l; // 饱和度为0 为灰色
    } else {
        const hue2rgb = (p, q, t) => {
            if (t < 0) t += 1;
            if (t > 1) t -= 1;
            if (t < 1 / 6) return p + (q - p) * 6 * t;
            if (t < 1 / 2) return q;
            if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
            return p;
        };
        const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
        const p = 2 * l - q;
        r = hue2rgb(p, q, h + 1 / 3);
        g = hue2rgb(p, q, h);
        b = hue2rgb(p, q, h - 1 / 3);
    }
    return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}

/**RGB到HSL的转换（把RGB转HSL后再改L值就可以修改颜色明暗度）*/
ColorManager.rgbToHsl = (r, g, b) => {
    r /= 255, g /= 255, b /= 255;
    const max = Math.max(r, g, b);
    const min = Math.min(r, g, b);
    let h, s, l = (max + min) / 2;
    if (max == min) {
        h = s = 0;
    } else {
        const d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);

        switch (max) {
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
            case g: h = (b - r) / d + 2; break;
            case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
    }
    return [h, s, l];
}
/**颜色取反 #00ff00->#ff00ff*/
ColorManager.invertColor=(color)=>{
    return "#" + ("000000" + (0xFFFFFF ^ parseInt(color.substring(1), 16)).toString(16)).slice(-6);
}
/**rgb转为10进制*/
ColorManager.rgbToDecimal = (r, g, b) => {
    var color = (r << 16) + (g << 8) + b;
    return color;
}
/**10进制转为rgb*/
ColorManager.decimalToRgb = (color) => {
    var r = color >> 16;
    var g = color >> 8 & 0xFF;
    var b = color & 0xFF;
    return [r, g, b];
}
/**带#的16进制转10进制(如'#ff00ff'转为16711935)*/
ColorManager.hexToDecimal = (hex) => {
    return parseInt(hex.replace('#', '0x'));
}
/**10进制转带#的16进制(如16711935转为'#ff00ff')*/
ColorManager.decimalToHex = (decimal) => {
    let str=decimal.toString(16).toUpperCase();
    while (str.length < 6){str = '0' + str;}
    return '#' + str
}
/**rgb转为带#16进制(如'#ff00ff')*/
ColorManager.rbgToHex=(r,b,g)=>{
    return ColorManager.decimalToHex(ColorManager.rgbToDecimal(r,b,g))
}
/**修改颜色的亮度（color为颜色值可以是多种格式，l为-1到1，负数是变暗，正数是变亮）
 * color=[255,255,255]/'#ffffff'/16777215/{r:255,g:255,b:255}
 * l为亮度 -1到1，负数是变暗，正数是变亮
 * type=0默认原来的格式,1:[r,g,b],2:#fffff,3:8421504,4:{r:25,g:25,b:25}
*/
ColorManager.changeColorLight = (color, l, type = 0) => {
    let value;
    if (Array.isArray(color)) {
        value = ctv(color);
    } else if (typeof color == 'string') {
        value = ctv(ColorManager.decimalToRgb(ColorManager.hexToDecimal(color)));
        if (type == 0) type = 2;
    } else if (typeof color == 'number') {
        value = ctv(ColorManager.decimalToRgb(color));
        if (type == 0) type = 3;
    } else if (typeof color == 'object') {
        value = ctv([color.r, color.g, color.b])
        if (type == 0) type = 4;
    }
    if (type > 0) {
        switch (type) {
            case 1://[25,25,25]
                break;
            case 2://#ffffff
                value = ColorManager.decimalToHex(ColorManager.rgbToDecimal(value[0], value[1], value[2]))
                break;
            case 3://8421504
                value = ColorManager.rgbToDecimal(value[0], value[1], value[2])
                break;
            case 4://{r:25,g:25,b:25}
                value = { r: value[0], g: value[1], b: value[2] }
                break;
        }
    }
    function ctv(color) {
        let v = ColorManager.rgbToHsl(color[0], color[1], color[2]);
        v[2] += l;
        v = ColorManager.hslToRgb(v[0], v[1], v[2]);
        return v;
    }
    return value;
}
/**随机颜色,默认返回带#的16进制颜色，参数为''时返回10进制颜色*/
ColorManager.randomColor=(type='#')=>{
    let color=Math.floor(Math.random()*0XFFFFFF).toString(16);
    while (color.length < 6){color = '0' + color;}
    return type==''?color:type+color;
}
/**主题色，背景色和前景色 */
ColorManager.themeColors=()=>{
    var colors=[['#FFFFFF','#000000'],['#000000','#FFFFFF'],['#57FF60','#004500'],['#FFFFFF','#5B8982'],['#CEC6B6','#45545F'],['#D47655','#E1F8E1'],['#E3927F','#223C5F'],['#F9ECDF','#825855'],['#EDEBE4','#417CA9'],['#333333','#aaaaaa'],['#bbbbbb','#222222']]
    return colors;
}
/**暖色到冷色*/
ColorManager.warnCoolColors=(selects)=>{
    var colors=['#FDFD03','#FC9904','#FD6605','#FC3204','#CD0204','#9C0163','#650266','#040263','#09399A','#046664','#349706','#63D000'];
    var temps=[];
    if(selects){
        for(var i=0;i<selects.length;i++){
            temps.push(colors[selects[i]]);
        }
    }else{
        temps=colors;
    }
    return temps;
}